/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gestion;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;
import org.apache.log4j.Logger;
import otros.Fecha;
import otros.MiTableModel;
import otros.Util;
import tablasSql.Clientes;
import tablasSql.Sql;

/**
 *
 * @author FERRETERIA
 */
public class FClientes extends javax.swing.JInternalFrame {

    private final static Logger log = Logger.getLogger(FClientes.class);
    private String quienLLamo;
    private JInternalFrame padre;
    private int pulsadoEnter = 0;
    /**
     * Creates new form FClientes
     */
    public FClientes() {
        init();
        quienLLamo = "";
    }
    
    public FClientes(JInternalFrame padre, String valor) {
        this.padre = padre;
        quienLLamo = valor;
        init();
    }
    
    private void init () {
        Util.setConfiguracionLog(Util.LOG4_FICH);
        //PropertyConfigurator.configure("log4j.properties");
        initComponents();
        activarCampos(true);
        vaciarCampos();
        bEditar.setEnabled(false);
        bBorrar.setEnabled(false);
        bBuscar.setFocusable(false);
        txtBuscar.requestFocus(true);
    }

    private void AnchoColumnas() {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel)tabla.getColumnModel();
        TableColumn col = colModel.getColumn(2);        
        col.setPreferredWidth(250);
        col = colModel.getColumn(3);        
        col.setPreferredWidth(250);
        col = colModel.getColumn(0);        
        col.setPreferredWidth(60);
        col = colModel.getColumn(1);        
        col.setPreferredWidth(100);
    }
    
    private void vaciarCampos() {
        txtCodigo.setText(String.valueOf(Sql.getMax("codigo","nclientes")+1));
        txtNombreComercial.setText("");
        txtNombreFiscal.setText("");
        txtDomicilio.setText("");
        txtLocalidad.setText("");
        txtCodigoPostal.setText("");
        txtNif.setText("");
        txtTelefono1.setText("");
        txtTelefono2.setText("");
        txtEmail.setText("");
        txtFax.setText("");
        txtUltimaFactura.setText("");
        txtUltimoAlbaran.setText("");
        txtDesPie.setText("0%");
        txtDesLinea.setText("0%");
        txtMargen.setText("0%");
        txtCopiaAlbaranes.setText("0");
        txtCopiaFactura.setText("0");
        txtNumFac.setText("");
        txtNumAlb.setText("");
        txtFechaCreado.setText("");
        cbTipoPago.setSelectedIndex(0);
        txtCredito.setValue(0);
        
        bEditar.setEnabled(false);
        bGuardar.setEnabled(true);
        bBorrar.setEnabled(false);

    }
    
     private void ActualizarCamposCliente (String codigo) {
        try {
            ResultSet res = Clientes.getCliente(codigo);
            res.next();
            txtCodigo.setText(String.valueOf(res.getInt("codigo")));
            txtNombreComercial.setText(res.getString("nombrecomercial"));
            txtNombreFiscal.setText(res.getString("nombrefiscal"));
            txtDomicilio.setText(res.getString("domicilio"));
            txtLocalidad.setText(res.getString("localidad"));
            txtCodigoPostal.setText(res.getString("codigopostal"));
            txtNif.setText(res.getString("nif"));
            txtTelefono1.setText(res.getString("telefono1"));
            txtTelefono2.setText(res.getString("telefono2"));
            txtEmail.setText(res.getString("email"));
            txtFax.setText(res.getString("fax"));
            txtUltimaFactura.setText(res.getString("idultfac"));
            txtUltimoAlbaran.setText(res.getString("idultalb"));
            txtCopiaAlbaranes.setText(res.getString("copiasalbaranes"));
            txtCopiaFactura.setText(res.getString("copiasfacturas"));
            txtDesPie.setText(res.getString("descPie")+ "%");
            txtDesLinea.setText(res.getString("descLinea")+ "%");
            txtMargen.setText(res.getString("margen")+ "%");
            cbTipoPago.setSelectedIndex(res.getInt("tipoPago"));
            txtCredito.setValue(res.getFloat("credito"));
            
            getCantidadFacAlb();
            
            String [] date;
            String fecha = "";
            if (res.getString("creado") != null) {
                date = res.getString("creado").split("-"); 
                fecha = date [2].substring(0, 2) + "/" + date[1] + "/" + date [0];
            }
            txtFechaCreado.setText(fecha);
            
            bEditar.setEnabled(true);
            bGuardar.setEnabled(false);
            bBorrar.setEnabled(true);
            
        } catch (SQLException ex) {
            log.warn (ex);
        }        
    }
    
    private void ActualizarTabla () {
         // se colocan los datos en la tabla
        String valor = txtBuscar.getText();

        MiTableModel datos = null;
        String criterios [] = {"codigo", "nombreComercial", "nombreFiscal", "nif"};
        
        for (int i = 0; ((i < criterios.length) && (datos == null)); i++) {
             datos = Clientes.getClientes(criterios[i], valor);
        }
        if (datos != null) {
            tabla.setModel(datos);
            AnchoColumnas();
            tabla.setAutoCreateRowSorter(true);
            tabla.requestFocus();
        }        
    }
    
    private void activarCampos (boolean activo) {
        txtCodigoPostal.setEditable(activo);
        txtCopiaAlbaranes.setEditable(activo);
        txtCopiaFactura.setEditable(activo);
        txtDesPie.setEditable(activo);
        txtDesLinea.setEditable(activo);
        txtMargen.setEditable(activo);
        txtDomicilio.setEditable(activo);
        txtEmail.setEditable(activo);
        txtFax.setEditable(activo);
        txtLocalidad.setEditable(activo);
        txtNif.setEditable(activo);
        txtNombreComercial.setEditable(activo);
        txtNombreFiscal.setEditable(activo);
        txtTelefono1.setEditable(activo);
        txtTelefono2.setEditable(activo); 
        cbTipoPago.setEnabled(activo);
        txtCredito.setEnabled(activo);
    }
    
    private void guardar () {
        if (bEditar.isEnabled()) {
            String [] datos = { txtNombreComercial.getText(), txtNombreFiscal.getText(), txtDomicilio.getText(),
                        txtLocalidad.getText(), txtCodigoPostal.getText() ,txtNif.getText(),
                        txtTelefono1.getText(),txtTelefono2.getText(), txtEmail.getText(),txtFax.getText(),
                        txtDesPie.getText().replace("%", "").trim(),txtDesLinea.getText().replace("%", "").trim(),
                        txtMargen.getText().replace("%", "").trim(),
                        txtCredito.getText().replace(",", ".").trim(),
                        txtCopiaAlbaranes.getText(), txtCopiaFactura.getText(), ""+cbTipoPago.getSelectedIndex(),
                        txtCodigo.getText()};
            Clientes.guardar(datos, false);
                        
        }
        else {
            String [] datos = { txtNombreComercial.getText(), txtNombreFiscal.getText(), txtDomicilio.getText(),
                        txtLocalidad.getText(), txtCodigoPostal.getText() ,txtNif.getText(),
                        txtTelefono1.getText(),txtTelefono2.getText(), txtEmail.getText(),txtFax.getText(), new Fecha().getFechaSQL(),
                        txtDesPie.getText().replace("%", "").trim(), txtDesLinea.getText().replace("%", "").trim(),
                        txtMargen.getText().replace("%", "").trim(),
                        txtCredito.getText().replace(",", ".").trim(),
                        txtCopiaAlbaranes.getText(), txtCopiaFactura.getText(), ""+cbTipoPago.getSelectedIndex(),
                        txtCodigo.getText()};
            Clientes.guardar(datos, true);
        }
        vaciarCampos();
        ActualizarTabla();
    }
    
    private void getCantidadFacAlb () {
        txtNumFac.setText("" + Clientes.getCantFac(Integer.parseInt(txtCodigo.getText()), 0));
        txtNumAlb.setText("" + Clientes.getCantFac(Integer.parseInt(txtCodigo.getText()), 1));
    }
    
    private void quienHaLlamado(int fila){
        switch (quienLLamo) {
            case Util.FTPV:
                ((FTPV)padre).getCliente(txtCodigo.getText());
                ((FTPV)padre).txtBuscar.requestFocus();
                this.dispose();
                break;
            case Util.FFACTURAS:
                Principal.fac.txtIdCliente.setText(tabla.getValueAt(fila, 0).toString());
                this.dispose();
                break;
            case "art":
                Principal.ac.txtCodigo.setText(tabla.getValueAt(fila, 0).toString());
                Principal.ac.txtNombreCli.setText(tabla.getValueAt(fila, 2).toString());
                this.dispose();
                break;
           
       }
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtBuscar = new javax.swing.JTextField();
        bBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtNombreComercial = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtNombreFiscal = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtDomicilio = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtLocalidad = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtNif = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtCopiaAlbaranes = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtCopiaFactura = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        txtUltimaFactura = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        txtUltimoAlbaran = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        txtDesPie = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        txtNumFac = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        txtNumAlb = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        txtFechaCreado = new javax.swing.JTextField();
        txtTelefono1 = new javax.swing.JFormattedTextField();
        txtTelefono2 = new javax.swing.JFormattedTextField();
        txtCodigoPostal = new javax.swing.JFormattedTextField();
        txtFax = new javax.swing.JFormattedTextField();
        txtDesLinea = new javax.swing.JTextField();
        jLabel21 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        txtMargen = new javax.swing.JTextField();
        cbTipoPago = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        pBotones = new javax.swing.JPanel();
        bEditar = new javax.swing.JButton();
        bGuardar = new javax.swing.JButton();
        bLimpiar = new javax.swing.JButton();
        bBorrar = new javax.swing.JButton();
        txtCredito = new javax.swing.JFormattedTextField();
        jLabel24 = new javax.swing.JLabel();

        setClosable(true);
        setTitle("Gestion Clientes");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/img/icono2.png"))); // NOI18N
        setPreferredSize(new java.awt.Dimension(650, 560));
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Criterio de busqueda: ");
        jPanel1.add(jLabel1);

        txtBuscar.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtBuscar.setPreferredSize(new java.awt.Dimension(250, 20));
        txtBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtBuscarKeyPressed(evt);
            }
        });
        jPanel1.add(txtBuscar);

        bBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/buscar.png"))); // NOI18N
        bBuscar.setToolTipText("Buscar");
        bBuscar.setMinimumSize(new java.awt.Dimension(51, 51));
        bBuscar.setPreferredSize(new java.awt.Dimension(56, 56));
        bBuscar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bBuscarMouseClicked(evt);
            }
        });
        jPanel1.add(bBuscar);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        getContentPane().add(jPanel1, gridBagConstraints);

        jScrollPane1.setMinimumSize(new java.awt.Dimension(300, 125));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(300, 125));

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null}
            },
            new String [] {
                "Codigo", "NIF / DNI", "Nombre Comercial", "Nombre Fiscal", "Telefono"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabla.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        tabla.getTableHeader().setReorderingAllowed(false);
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMouseClicked(evt);
            }
        });
        tabla.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tablaKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tablaKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tabla);
        if (tabla.getColumnModel().getColumnCount() > 0) {
            tabla.getColumnModel().getColumn(0).setPreferredWidth(50);
            tabla.getColumnModel().getColumn(2).setPreferredWidth(250);
            tabla.getColumnModel().getColumn(3).setPreferredWidth(250);
        }

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 10);
        getContentPane().add(jScrollPane1, gridBagConstraints);

        jLabel2.setText("Codigo");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        getContentPane().add(jLabel2, gridBagConstraints);

        txtCodigo.setEditable(false);
        txtCodigo.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCodigo.setMinimumSize(new java.awt.Dimension(75, 20));
        txtCodigo.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(txtCodigo, gridBagConstraints);

        jLabel3.setText("Nombre Comercial");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 0, 5);
        getContentPane().add(jLabel3, gridBagConstraints);

        txtNombreComercial.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNombreComercial.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtNombreComercialMouseClicked(evt);
            }
        });
        txtNombreComercial.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtNombreComercialKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(txtNombreComercial, gridBagConstraints);

        jLabel4.setText("Nombre Fiscal");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        getContentPane().add(jLabel4, gridBagConstraints);

        txtNombreFiscal.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNombreFiscal.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtNombreFiscalMouseClicked(evt);
            }
        });
        txtNombreFiscal.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtNombreFiscalKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(txtNombreFiscal, gridBagConstraints);

        jLabel5.setText("Domicilio");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        getContentPane().add(jLabel5, gridBagConstraints);

        txtDomicilio.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtDomicilio.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtDomicilioMouseClicked(evt);
            }
        });
        txtDomicilio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDomicilioKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(txtDomicilio, gridBagConstraints);

        jLabel6.setText("Localidad");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        getContentPane().add(jLabel6, gridBagConstraints);

        txtLocalidad.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtLocalidad.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtLocalidadMouseClicked(evt);
            }
        });
        txtLocalidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtLocalidadKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(txtLocalidad, gridBagConstraints);

        jLabel7.setText("Codigo Postal");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 3, 0, 5);
        getContentPane().add(jLabel7, gridBagConstraints);

        jLabel8.setText("NIF / DNI");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        getContentPane().add(jLabel8, gridBagConstraints);

        txtNif.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNif.setMinimumSize(new java.awt.Dimension(95, 20));
        txtNif.setPreferredSize(new java.awt.Dimension(95, 20));
        txtNif.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtNifMouseClicked(evt);
            }
        });
        txtNif.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtNifKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(txtNif, gridBagConstraints);

        jLabel9.setText("Telefonos");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        getContentPane().add(jLabel9, gridBagConstraints);

        jLabel11.setText("email");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        getContentPane().add(jLabel11, gridBagConstraints);

        txtEmail.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtEmail.setPreferredSize(new java.awt.Dimension(100, 20));
        txtEmail.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtEmailMouseClicked(evt);
            }
        });
        txtEmail.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtEmailKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(txtEmail, gridBagConstraints);

        jLabel12.setText("Copias Albaranes");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        getContentPane().add(jLabel12, gridBagConstraints);

        txtCopiaAlbaranes.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCopiaAlbaranes.setText("0");
        txtCopiaAlbaranes.setMinimumSize(new java.awt.Dimension(40, 20));
        txtCopiaAlbaranes.setPreferredSize(new java.awt.Dimension(40, 20));
        txtCopiaAlbaranes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtCopiaAlbaranesMouseClicked(evt);
            }
        });
        txtCopiaAlbaranes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCopiaAlbaranesKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(txtCopiaAlbaranes, gridBagConstraints);

        jLabel13.setText("Copias Facturas");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        getContentPane().add(jLabel13, gridBagConstraints);

        txtCopiaFactura.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCopiaFactura.setText("0");
        txtCopiaFactura.setMinimumSize(new java.awt.Dimension(40, 20));
        txtCopiaFactura.setPreferredSize(new java.awt.Dimension(40, 20));
        txtCopiaFactura.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtCopiaFacturaMouseClicked(evt);
            }
        });
        txtCopiaFactura.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCopiaFacturaKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(txtCopiaFactura, gridBagConstraints);

        jLabel14.setText("Ultima Factura");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 10, 5);
        getContentPane().add(jLabel14, gridBagConstraints);

        txtUltimaFactura.setEditable(false);
        txtUltimaFactura.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtUltimaFactura.setToolTipText("click: abre la factura");
        txtUltimaFactura.setMinimumSize(new java.awt.Dimension(150, 20));
        txtUltimaFactura.setPreferredSize(new java.awt.Dimension(150, 20));
        txtUltimaFactura.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtUltimaFacturaMouseClicked(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 10, 0);
        getContentPane().add(txtUltimaFactura, gridBagConstraints);

        jLabel15.setText("Ultimo Albaran");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 10, 5);
        getContentPane().add(jLabel15, gridBagConstraints);

        txtUltimoAlbaran.setEditable(false);
        txtUltimoAlbaran.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtUltimoAlbaran.setToolTipText("click: abre la albaran");
        txtUltimoAlbaran.setMinimumSize(new java.awt.Dimension(150, 20));
        txtUltimoAlbaran.setPreferredSize(new java.awt.Dimension(150, 20));
        txtUltimoAlbaran.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtUltimoAlbaranMouseClicked(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 10, 0);
        getContentPane().add(txtUltimoAlbaran, gridBagConstraints);

        jLabel16.setText("Descuentos:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        getContentPane().add(jLabel16, gridBagConstraints);

        txtDesPie.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtDesPie.setText("0%");
        txtDesPie.setMinimumSize(new java.awt.Dimension(40, 20));
        txtDesPie.setPreferredSize(new java.awt.Dimension(40, 20));
        txtDesPie.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtDesPieMouseClicked(evt);
            }
        });
        txtDesPie.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDesPieKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 10);
        getContentPane().add(txtDesPie, gridBagConstraints);

        jLabel17.setText("Fax");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        getContentPane().add(jLabel17, gridBagConstraints);

        jLabel18.setText("Nº Facturas");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        getContentPane().add(jLabel18, gridBagConstraints);

        txtNumFac.setEditable(false);
        txtNumFac.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNumFac.setMinimumSize(new java.awt.Dimension(75, 20));
        txtNumFac.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(txtNumFac, gridBagConstraints);

        jLabel19.setText("Nº Albaranes");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        getContentPane().add(jLabel19, gridBagConstraints);

        txtNumAlb.setEditable(false);
        txtNumAlb.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNumAlb.setMinimumSize(new java.awt.Dimension(75, 20));
        txtNumAlb.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(txtNumAlb, gridBagConstraints);

        jLabel20.setText("Creado");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        getContentPane().add(jLabel20, gridBagConstraints);

        txtFechaCreado.setEditable(false);
        txtFechaCreado.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtFechaCreado.setMinimumSize(new java.awt.Dimension(75, 20));
        txtFechaCreado.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(txtFechaCreado, gridBagConstraints);

        try {
            txtTelefono1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#########")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtTelefono1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtTelefono1.setText("");
        txtTelefono1.setMinimumSize(new java.awt.Dimension(75, 20));
        txtTelefono1.setPreferredSize(new java.awt.Dimension(75, 20));
        txtTelefono1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtTelefono1MouseClicked(evt);
            }
        });
        txtTelefono1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtTelefono1KeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(txtTelefono1, gridBagConstraints);

        try {
            txtTelefono2.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#########")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtTelefono2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtTelefono2.setText("");
        txtTelefono2.setMinimumSize(new java.awt.Dimension(75, 20));
        txtTelefono2.setPreferredSize(new java.awt.Dimension(75, 20));
        txtTelefono2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtTelefono2MouseClicked(evt);
            }
        });
        txtTelefono2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtTelefono2KeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 10);
        getContentPane().add(txtTelefono2, gridBagConstraints);

        try {
            txtCodigoPostal.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtCodigoPostal.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCodigoPostal.setText("");
        txtCodigoPostal.setMinimumSize(new java.awt.Dimension(60, 20));
        txtCodigoPostal.setPreferredSize(new java.awt.Dimension(60, 20));
        txtCodigoPostal.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtCodigoPostalMouseClicked(evt);
            }
        });
        txtCodigoPostal.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCodigoPostalKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(txtCodigoPostal, gridBagConstraints);

        try {
            txtFax.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#########")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtFax.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtFax.setText("");
        txtFax.setMinimumSize(new java.awt.Dimension(75, 20));
        txtFax.setPreferredSize(new java.awt.Dimension(75, 20));
        txtFax.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtFaxMouseClicked(evt);
            }
        });
        txtFax.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtFaxKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        getContentPane().add(txtFax, gridBagConstraints);

        txtDesLinea.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtDesLinea.setText("0%");
        txtDesLinea.setMinimumSize(new java.awt.Dimension(40, 20));
        txtDesLinea.setPreferredSize(new java.awt.Dimension(40, 20));
        txtDesLinea.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtDesLineaMouseClicked(evt);
            }
        });
        txtDesLinea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDesLineaKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 10);
        getContentPane().add(txtDesLinea, gridBagConstraints);

        jLabel21.setText("Pie");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        getContentPane().add(jLabel21, gridBagConstraints);

        jLabel22.setText("Linea");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        getContentPane().add(jLabel22, gridBagConstraints);

        jLabel23.setText("Margen");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 10, 0);
        getContentPane().add(jLabel23, gridBagConstraints);

        txtMargen.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtMargen.setText("0%");
        txtMargen.setMinimumSize(new java.awt.Dimension(40, 20));
        txtMargen.setPreferredSize(new java.awt.Dimension(40, 20));
        txtMargen.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtMargenMouseClicked(evt);
            }
        });
        txtMargen.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtMargenKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 50, 10, 10);
        getContentPane().add(txtMargen, gridBagConstraints);

        cbTipoPago.setMaximumRowCount(4);
        cbTipoPago.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Factura a Contado", "Factura a Credito", "Albaran a Credito", "Fiado", " " }));
        cbTipoPago.setMinimumSize(new java.awt.Dimension(132, 20));
        cbTipoPago.setPreferredSize(new java.awt.Dimension(132, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(cbTipoPago, gridBagConstraints);

        jLabel10.setText("Tipo de pago");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        getContentPane().add(jLabel10, gridBagConstraints);

        pBotones.setMinimumSize(new java.awt.Dimension(320, 80));
        pBotones.setPreferredSize(new java.awt.Dimension(320, 80));
        pBotones.setLayout(new java.awt.GridBagLayout());

        bEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/editar.png"))); // NOI18N
        bEditar.setToolTipText("Editar");
        bEditar.setMinimumSize(new java.awt.Dimension(60, 60));
        bEditar.setPreferredSize(new java.awt.Dimension(60, 60));
        bEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEditarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        pBotones.add(bEditar, gridBagConstraints);

        bGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/guardar.png"))); // NOI18N
        bGuardar.setToolTipText("Guardar");
        bGuardar.setMinimumSize(new java.awt.Dimension(60, 60));
        bGuardar.setPreferredSize(new java.awt.Dimension(60, 60));
        bGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bGuardarActionPerformed(evt);
            }
        });
        bGuardar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                bGuardarKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 30, 0, 0);
        pBotones.add(bGuardar, gridBagConstraints);

        bLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        bLimpiar.setToolTipText("Limpiar");
        bLimpiar.setMinimumSize(new java.awt.Dimension(60, 60));
        bLimpiar.setPreferredSize(new java.awt.Dimension(60, 60));
        bLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLimpiarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 30, 0, 0);
        pBotones.add(bLimpiar, gridBagConstraints);

        bBorrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/eliminar.png"))); // NOI18N
        bBorrar.setToolTipText("Borrar");
        bBorrar.setMinimumSize(new java.awt.Dimension(60, 60));
        bBorrar.setPreferredSize(new java.awt.Dimension(60, 60));
        bBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBorrarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 150, 0, 0);
        pBotones.add(bBorrar, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.gridwidth = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(pBotones, gridBagConstraints);

        txtCredito.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        txtCredito.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCredito.setMinimumSize(new java.awt.Dimension(60, 20));
        txtCredito.setPreferredSize(new java.awt.Dimension(60, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        getContentPane().add(txtCredito, gridBagConstraints);

        jLabel24.setText("Credito");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 3, 0, 5);
        getContentPane().add(jLabel24, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tablaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseClicked
        int fila = tabla.rowAtPoint(evt.getPoint());
        int columna = tabla.columnAtPoint(evt.getPoint());
        if ((fila > -1) && (columna > -1)) {
            if (tabla.getValueAt(fila,columna)!= null) {
                String codigo = tabla.getValueAt(fila, 0).toString();
                ActualizarCamposCliente(codigo);
                activarCampos(false);
                bEditar.requestFocus();
            }
        }
        if (evt.getClickCount() == 2) {
            quienHaLlamado(fila);
        }
    }//GEN-LAST:event_tablaMouseClicked

    private void bBuscarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bBuscarMouseClicked
        ActualizarTabla();
    }//GEN-LAST:event_bBuscarMouseClicked

    private void txtBuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarKeyPressed
        if (evt.getKeyCode() == 10) {
            ActualizarTabla();
            pulsadoEnter = 0;
        }
    }//GEN-LAST:event_txtBuscarKeyPressed

    private void tablaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tablaKeyPressed
        if (evt.getKeyCode() == 10) {
            int fila = tabla.getSelectedRow();
            int columna = tabla.getSelectedColumn();
            if ((fila > -1) && (columna > -1)) {
                if (tabla.getValueAt(fila,columna)!= null) {
                    String codigo = tabla.getValueAt(fila, 0).toString();
                    ActualizarCamposCliente(codigo);
                    activarCampos(false);
                }     
            }
            pulsadoEnter++;
            evt.consume();
        }
    }//GEN-LAST:event_tablaKeyPressed

    private void txtNifKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNifKeyPressed
        if (evt.getKeyCode() == 10) 
            txtNombreFiscal.requestFocus();
    }//GEN-LAST:event_txtNifKeyPressed

    private void txtNombreFiscalKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNombreFiscalKeyPressed
        if (evt.getKeyCode() == 10) 
            txtNombreComercial.requestFocus();
    }//GEN-LAST:event_txtNombreFiscalKeyPressed

    private void txtNombreComercialKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNombreComercialKeyPressed
        if (evt.getKeyCode() == 10) 
            txtDomicilio.requestFocus();
    }//GEN-LAST:event_txtNombreComercialKeyPressed

    private void txtDomicilioKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDomicilioKeyPressed
        if (evt.getKeyCode() == 10) 
            txtCodigoPostal.requestFocus();
    }//GEN-LAST:event_txtDomicilioKeyPressed

    private void txtCodigoPostalKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodigoPostalKeyPressed
        if (evt.getKeyCode() == 10) 
            txtLocalidad.requestFocus();
    }//GEN-LAST:event_txtCodigoPostalKeyPressed

    private void txtLocalidadKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtLocalidadKeyPressed
        if (evt.getKeyCode() == 10) 
            txtFax.requestFocus();
    }//GEN-LAST:event_txtLocalidadKeyPressed

    private void txtFaxKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtFaxKeyPressed
        if (evt.getKeyCode() == 10) 
            txtEmail.requestFocus();
    }//GEN-LAST:event_txtFaxKeyPressed

    private void txtEmailKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtEmailKeyPressed
        if (evt.getKeyCode() == 10) 
            txtTelefono1.requestFocus();
    }//GEN-LAST:event_txtEmailKeyPressed

    private void txtTelefono1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTelefono1KeyPressed
        if (evt.getKeyCode() == 10) 
            txtTelefono2.requestFocus();
    }//GEN-LAST:event_txtTelefono1KeyPressed

    private void txtTelefono2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTelefono2KeyPressed
        if (evt.getKeyCode() == 10) 
            txtCopiaAlbaranes.requestFocus();
    }//GEN-LAST:event_txtTelefono2KeyPressed

    private void txtDesPieKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDesPieKeyPressed
        if (evt.getKeyCode() == 10) {
            txtDesPie.setText(txtDesPie.getText() + "%");
            txtDesLinea.requestFocus();
        }
    }//GEN-LAST:event_txtDesPieKeyPressed

    private void txtCopiaAlbaranesKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCopiaAlbaranesKeyPressed
        if (evt.getKeyCode() == 10) 
            txtCopiaFactura.requestFocus();
    }//GEN-LAST:event_txtCopiaAlbaranesKeyPressed

    private void txtCopiaFacturaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCopiaFacturaKeyPressed
        if (evt.getKeyCode() == 10) 
            txtDesPie.requestFocus();
    }//GEN-LAST:event_txtCopiaFacturaKeyPressed

    private void bGuardarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_bGuardarKeyPressed
        if (evt.getKeyCode() == 10) 
           guardar();
    }//GEN-LAST:event_bGuardarKeyPressed

    private void txtDesLineaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDesLineaKeyPressed
        if (evt.getKeyCode() == 10) {
            txtDesLinea.setText(txtDesLinea.getText() + "%");
            txtMargen.requestFocus();
        }
    }//GEN-LAST:event_txtDesLineaKeyPressed

    private void txtDesPieMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtDesPieMouseClicked
        txtDesPie.selectAll();
    }//GEN-LAST:event_txtDesPieMouseClicked

    private void txtDesLineaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtDesLineaMouseClicked
        txtDesLinea.selectAll();
    }//GEN-LAST:event_txtDesLineaMouseClicked

    private void txtNifMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtNifMouseClicked
        txtNif.selectAll();
    }//GEN-LAST:event_txtNifMouseClicked

    private void txtNombreFiscalMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtNombreFiscalMouseClicked
        txtNombreFiscal.selectAll();
    }//GEN-LAST:event_txtNombreFiscalMouseClicked

    private void txtNombreComercialMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtNombreComercialMouseClicked
        txtNombreComercial.selectAll();
    }//GEN-LAST:event_txtNombreComercialMouseClicked

    private void txtDomicilioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtDomicilioMouseClicked
        txtDomicilio.selectAll();
    }//GEN-LAST:event_txtDomicilioMouseClicked

    private void txtLocalidadMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtLocalidadMouseClicked
        txtLocalidad.selectAll();
    }//GEN-LAST:event_txtLocalidadMouseClicked

    private void txtEmailMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtEmailMouseClicked
        txtEmail.selectAll();
    }//GEN-LAST:event_txtEmailMouseClicked

    private void txtCopiaAlbaranesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtCopiaAlbaranesMouseClicked
        txtCopiaAlbaranes.selectAll();
    }//GEN-LAST:event_txtCopiaAlbaranesMouseClicked

    private void txtCopiaFacturaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtCopiaFacturaMouseClicked
        txtCopiaFactura.selectAll();
    }//GEN-LAST:event_txtCopiaFacturaMouseClicked

    private void txtCodigoPostalMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtCodigoPostalMouseClicked
        txtCodigoPostal.selectAll();
    }//GEN-LAST:event_txtCodigoPostalMouseClicked

    private void txtFaxMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtFaxMouseClicked
        txtFax.selectAll();
    }//GEN-LAST:event_txtFaxMouseClicked

    private void txtTelefono1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtTelefono1MouseClicked
        txtTelefono1.selectAll();
    }//GEN-LAST:event_txtTelefono1MouseClicked

    private void txtTelefono2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtTelefono2MouseClicked
        txtTelefono2.selectAll();
    }//GEN-LAST:event_txtTelefono2MouseClicked

    private void txtUltimaFacturaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtUltimaFacturaMouseClicked
        if(txtUltimaFactura.getText().length() == 20) {
            FTPV f = new FTPV();
            this.getParent().add(f);
            Principal.centrarFrame(f);
            f.show();
            f.cargarNumTicket(txtUltimaFactura.getText(), false);
        }  
    }//GEN-LAST:event_txtUltimaFacturaMouseClicked

    private void txtUltimoAlbaranMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtUltimoAlbaranMouseClicked
        if(txtUltimoAlbaran.getText().length() == 20) {
            FTPV f = new FTPV();
            this.getParent().add(f);
            Principal.centrarFrame(f);
            //String año = txtUltimoAlbaran.getText().substring(0, 4);
            //String ticket = txtUltimoAlbaran.getText().substring(6, 16);
            f.show();
            f.cargarNumTicket(txtUltimaFactura.getText(), true);
        }
    }//GEN-LAST:event_txtUltimoAlbaranMouseClicked

    private void bLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLimpiarActionPerformed
        vaciarCampos();
        activarCampos(true);
    }//GEN-LAST:event_bLimpiarActionPerformed

    private void txtMargenMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtMargenMouseClicked
        txtMargen.selectAll();
    }//GEN-LAST:event_txtMargenMouseClicked

    private void txtMargenKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMargenKeyPressed
         if (evt.getKeyCode() == 10) {
             txtMargen.setText(txtMargen.getText() + "%");
             bGuardar.requestFocus();
         }
    }//GEN-LAST:event_txtMargenKeyPressed

    private void tablaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tablaKeyReleased
        if(evt.getKeyCode()==10) {
            if (pulsadoEnter > 1) {
                quienHaLlamado(tabla.getSelectedRow());
                pulsadoEnter = 0;
            }
        }
    }//GEN-LAST:event_tablaKeyReleased

    private void bBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBorrarActionPerformed
        String tablas = Clientes.buscarClienteEnFacturas(txtCodigo.getText());
        if (!tablas.isEmpty()) 
            JOptionPane.showMessageDialog(this, "El cliente no se puede borrar porque tiene "+tablas,
                            "Eliminar cliente", JOptionPane.WARNING_MESSAGE);
        else
            if(JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "¿Esta seguro de borrar?","Borrar", JOptionPane.OK_CANCEL_OPTION)) {
                Clientes.borrar(txtCodigo.getText());
                vaciarCampos();
                txtBuscar.setText("");
                ActualizarTabla();
                activarCampos(true);
            }
    }//GEN-LAST:event_bBorrarActionPerformed

    private void bGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGuardarActionPerformed
        guardar();
    }//GEN-LAST:event_bGuardarActionPerformed

    private void bEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEditarActionPerformed
        activarCampos(!txtNif.isEditable());
        bBorrar.setEnabled(!txtNif.isEditable());
        bGuardar.setEnabled(txtNif.isEditable());
    }//GEN-LAST:event_bEditarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBorrar;
    private javax.swing.JButton bBuscar;
    private javax.swing.JButton bEditar;
    private javax.swing.JButton bGuardar;
    private javax.swing.JButton bLimpiar;
    private javax.swing.JComboBox cbTipoPago;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pBotones;
    private javax.swing.JTable tabla;
    private javax.swing.JTextField txtBuscar;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JFormattedTextField txtCodigoPostal;
    private javax.swing.JTextField txtCopiaAlbaranes;
    private javax.swing.JTextField txtCopiaFactura;
    private javax.swing.JFormattedTextField txtCredito;
    private javax.swing.JTextField txtDesLinea;
    private javax.swing.JTextField txtDesPie;
    private javax.swing.JTextField txtDomicilio;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JFormattedTextField txtFax;
    private javax.swing.JTextField txtFechaCreado;
    private javax.swing.JTextField txtLocalidad;
    private javax.swing.JTextField txtMargen;
    private javax.swing.JTextField txtNif;
    private javax.swing.JTextField txtNombreComercial;
    private javax.swing.JTextField txtNombreFiscal;
    private javax.swing.JTextField txtNumAlb;
    private javax.swing.JTextField txtNumFac;
    private javax.swing.JFormattedTextField txtTelefono1;
    private javax.swing.JFormattedTextField txtTelefono2;
    private javax.swing.JTextField txtUltimaFactura;
    private javax.swing.JTextField txtUltimoAlbaran;
    // End of variables declaration//GEN-END:variables
}
